/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <gtest/gtest.h>
#include <test_utilities/test_base.h>

#include "devmon.h"

const std::string TEST_SERVICE_NAME = "bmc.kepler.test_i2c";

using namespace devmon;

class TestI2c : public mc::test::TestWithEngine {
public:
protected:
    static void SetUpTestSuite() {
        TestWithEngine::SetUpTestSuite();
        m_test_service.init({{"driver_path", "./plugins"}});
        m_test_service.start();
    }

    static void TearDownTestSuite() {
        m_test_service.stop();
        TestWithEngine::TearDownTestSuite();
    }

    void SetUp() override {
    }

    void TearDown() override {
    }

    static devmon::devmon_service m_test_service;
};

// 在类外部定义和初始化静态成员
devmon::devmon_service TestI2c::m_test_service{TEST_SERVICE_NAME};

TEST_F(TestI2c, add_i2c_bus) {
    mc::dict    connector = {{"GroupPosition", 10}, {"SystemId", "1"}, {"Slot", "10"}};
    std::string csr_file  = "../tests/tests_data/csr/hisi_182x_pcie_nic_card.sr";

    auto strand = mc::engine::make_strand();
    auto conn   = mc::dbus::connection::open_session_bus(strand);
    conn->start();
    auto msg =
        mc::dbus::message::new_method_call(TEST_SERVICE_NAME, "/bmc/dev", "bmc.dev", "AddDevice");
    msg.set_sender(TEST_SERVICE_NAME);
    msg.set_serial(1);
    auto writer = msg.writer();
    writer << csr_file << connector;

    auto reply = conn->send_with_reply(std::move(msg), mc::milliseconds(1000));
    ASSERT_TRUE(reply.is_valid() && reply.is_method_return());
}